
package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.plugin.single.privilege.core.entity.LanguageLabel;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.LanguageLabelService;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;


@Controller
@RequestMapping("/languageLabel")
public class LanguageLabelController extends BasicController {
   @Autowired
   private LanguageLabelService languageLabelService;
    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        return "single/language/languageLabel";
    }

    @RequestMapping(value = VIEW_FILED + "/list")
    public
    @ResponseBody
    GridVo list(HttpServletRequest request, String code,String name ) throws ServletRequestBindingException {
        Condition condition = new Condition();
        if (StringUtils.isNoneBlank(code)) {
            condition.put("code", Condition.LIKE, code);
        }
        if (StringUtils.isNoneBlank(name)) {
            condition.put("name", Condition.LIKE, name);
        }
        GridVo vo = new GridVo();
        Page<LanguageLabel> pageResult = languageLabelService.selectPage(condition, null, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));

        return new GridVo(pageResult);
    }

    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid LanguageLabel languageLabel) {
        Date date = new Date();
         if(StringUtils.isEmpty(languageLabel.getName())||StringUtils.isEmpty(languageLabel.getCode())){
             return new Message(StatusType.ERROR.getKey(), "参数错误");
         }
        languageLabel.setModifyTime(date);
        languageLabel.setModifyBy(getLoginName());
        languageLabel.setCreateBy(getLoginName());
        languageLabel.setCreateTime(date);
        languageLabel.setId(IdUtils64.getInstance().getStringId());
        if(StringUtils.isEmpty(languageLabel.getIsEnabled())){
            languageLabel.setIsEnabled(BaseEnum.IS_ENABLED.ENABLED.getKey());
        }
        languageLabelService.insert(languageLabel);
        return new Message(StatusType.SUCCESS.getKey(), "保存成功");
    }
    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/edit")
    public String edit( Model model, String type,String id) {
        if ("edit".equals(type)) {
            if(StringUtils.isNotEmpty(id)){
                LanguageLabel languageLabel= languageLabelService.selectByPrimaryKey(id);
                model.addAttribute("languageLabel",languageLabel);
            }
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        return "single/language/languageLabelEdit";
    }
    /**
     * update
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid LanguageLabel languageLabel) {
        Assert.notNull(languageLabel.getId(), "参数错误");
        LanguageLabel updateLanguageLabel = languageLabelService.selectByPrimaryKey(languageLabel.getId());
        if (updateLanguageLabel != null) {
            updateLanguageLabel.setIsEnabled(languageLabel.getIsEnabled());
            updateLanguageLabel.setDescription(languageLabel.getDescription());
            updateLanguageLabel.setModifyBy(getLoginName());
            updateLanguageLabel.setModifyTime(new Date());
            updateLanguageLabel.setCode(languageLabel.getCode());
            updateLanguageLabel.setName(languageLabel.getName());
            updateLanguageLabel.setAppCode(languageLabel.getAppCode());
            languageLabelService.updateByPrimaryKeySelective(updateLanguageLabel);
            return new Message(StatusType.SUCCESS.getKey(), "更新成功");
        }
        return new Message(StatusType.ERROR.getKey(), "更新失败");
    }
    @RequestMapping(DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
         if(ArrayUtils.isNotEmpty(ids)){
             languageLabelService.delete(ids);
             return new Message(StatusType.SUCCESS.getKey(), "删除成功");
         }
        return new Message(StatusType.ERROR.getKey(), "删除失败");
    }

}